<?php
/**
 * 命令行具体执行程序
 * 1)必须放置于cli/script目录下
 * 2)文件名必须为小写的cli_xxxx.php
 * 3)类名必须为Cli_Xxxx格式
 * 4)必须包含一个run($args)方法,$args为自动转换后的命令行参数
 *
 * 调用方法:
 * <code>
 * cli script=demo username=sharmy
 * </code>
 *
 * 命令行应用示例
 *
 * @author Chengtian.Hu<chengtian.hu@gmail.com>
 */

class Cli_agentpay {
	public function run($args) {
		$ids = array (
				'20331155139',
				'18286818217',
				'18286774453' 
		);
		set_time_limit ( 0 );
		
		require_once dirname ( __FILE__ ) . '/../../lib/top/TopClient.php';
		require_once dirname ( __FILE__ ) . '/../../lib/top/request/TradeFullinfoGetRequest.php';
		require_once dirname ( __FILE__ ) . '/../../lib/top/request/LogisticsDummySendRequest.php';
		require_once dirname ( __FILE__ ) . '/../../lib/top/RequestCheckUtil.php';
		require_once dirname ( __FILE__ ) . '/../../lib/top/request/TradesSoldGetRequest.php';
		$session = Acct::find ( "tbtoken<>''" )->getAll();
		dump($session);
		foreach ( $session as $item ) {
			$c = new TopClient ();
			$c->appkey = '21612259';
			$c->secretKey = '39a31b83172056116d82ac8af7ad7a4b';
			$req = new TradesSoldGetRequest ();
			$req->setFields ( 'status,num,total_fee,price,num_iid,receiver_address,tid,buyer_nick' );
			$req->setStartCreated ( date ( 'Y-m-d', strtotime ( '-3 day' ) ) );
			$req->setStatus ( 'WAIT_SELLER_SEND_GOODS' );
			$querytaobao = $c->execute ( $req, $item->tbtoken );
			Helper_Common::log ( 'TAOBAO-CHECK RESULT:' . print_r ( $querytaobao, true ),__CLASS__ );
			dump ( ($querytaobao) );
			if (intval ( $querytaobao->total_results ) > 0) {
				$trades = array ();
				if (intval ( $querytaobao->total_results ) == 1) {
					$trades = array (
							$querytaobao->trades->trade 
					);
				} else {
					$trades = $querytaobao->trades->trade;
				}
				// dump($trades);die;
				foreach ( $trades as $k => $v ) {
					if (! in_array ( $v->num_iid, $ids )) {
						$taobao_nick = $v->buyer_nick;
						$acct = Acct::find ( 'buyer_nick = ? and taobao_flag = 1 and agent_id = ?', $taobao_nick,$item->id )->getOne ();
						
						if ($acct->id ()) {
							$orderid = $v->tid;
							$querypwd = md5 ( time () );
							$req = new LogisticsDummySendRequest ();
							$req->setTid ( $v->tid );
							$resp = $c->execute ( $req, $item->tbtoken );
							if ($resp->shipping->is_success) {
								// Helper_Common::log("TAOBAO SEND
								// OK,ORDERDATA=".print_r($querytaobao,true),'taobao');
								$cardinfo = '';
								
								if($acct->is_Onelevel == true)
								{
									$agent_id = -1;
								}else{
									$agent_id = $item->id;
								}
								for($i = 1; $i <= $v->num; $i ++) {
									$card = array (
											'cardname' => $orderid . '_' . $i,
											'password' => $querypwd,
											'balance' => $v->price,
											'voucher_acctname' => '',
											'vouchered_acctname' => '',
											'vouchereddate' => '0000-00-00',
											'agent_id' => $agent_id,
											'create_acctname' => 'taobao' 
									);
									dump($card);
									Helper_Common::log ( 'TAOBAO-make card RESULT:' . print_r ( $card, true ),__CLASS__ );
									// $cardinfo .=
									// sprintf("%s,%s<br>",$orderid.'_'.$i,$querypwd);
									Acct::make_agentcard ( $card );
									$status = Acct::use_agentcard ( $acct->id, $card ['cardname'], $card ['password'] );
									Helper_Common::log ( 'TAOBAO-make card use:' . print_r ( $status, true ),__CLASS__ );
									if ($status === true) {
										$acct_name = $acct->acctname;
										$log = new Syslog ();
										$log->acctname = $acct_name;
										$log->admin_id = 1;
										$log->agent_id = $acct->id;
										$log->clientip = '';
										$log->logtext = "后台自动为代理充值卡{$card['cardname']} 为 {$acct_name} 充值{$card['balance']}成功";
										$log->save ( 0, 'create' );
									
									}
								}
							}
						}
					}
				}
			}
		}
	
	}
	function _loadtoken() {
		$taobao = Taobaotoken::find ()->getOne ();
		return trim ( $taobao->token );
	}
}